package com.laihui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.StringUtils;
import com.laihui.Constants;
import com.laihui.MainActivity;
import com.laihui.MyApplication;
import com.laihui.base.BaseActivity;
import com.laihui.entity.Login;
import com.laihui.entity.RetMap;
import com.laihui.utils.CodeUtils;
import com.laihui.utils.LoginPwdFormatUtils;
import com.laihui.utils.ToastUtil;
import com.laihui.view.TimeButton;
import com.laihui.view.TopBar;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 注册第一个页面
 */
public class RegistActivityStep2 extends BaseActivity {

    @BindView(R.id.topbar)//topbar
            TopBar topBar;
    @BindView(R.id.tv_phone)//手机号
            TextView tvPhone;
    @BindView(R.id.et_phone_code)//手机号
            EditText etPhoneCode;



    @BindView(R.id.bt_get_code)//验证码
            TimeButton timeButton;


    CodeUtils codeUtils;
    private Activity mActivity;
    private MyApplication application;
    private static String Tag = "RegistActivity";
    private LoginPwdFormatUtils loginPwdFormatUtils;

    @Override
    public void init() {
        mActivity = RegistActivityStep2.this;
        application = (MyApplication) getApplication();

        topBar.setRightButton(true);

        topBar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                startActivity(new Intent(mActivity, MainActivity.class));
                finish();
            }

            @Override
            public void rightClick() {
                startActivity(new Intent(mActivity, LoginActivity.class));
            }
        });

        tvPhone.setText(application.username);
        timeButton.setMobile(application.username);

        getcode();

    }

    @SuppressLint("LongLogTag")
    private void getcode() {
        String member_id = application.userId;
        String mobile = application.username;
        HashMap map = new HashMap();
        map.put("member_id",member_id);
        map.put("mobile",mobile);

        application.apiService.RegisterCode(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RetMap>() {
                    @Override
                    public void onCompleted() {
                        //dismissProgress();
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e("loginFailed", e.toString()

                        );
                    }
                    @Override
                    public void onNext(RetMap user) {
                        ToastUtil.showToast(RegistActivityStep2.this,user.message);

                        if(user.status.equals("1")) {
                            timeButton.start();
                        }
                    }
                });

    }

    @SuppressLint("LongLogTag")
    private void checkcode() {
        String member_id = application.userId;
        String mobile = application.username;
        HashMap map = new HashMap();
        map.put("member_id",member_id);
        map.put("mobile",mobile);
        map.put("mobileCode",etPhoneCode.getText().toString());
        application.apiService.regcodeValidate(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RetMap>() {
                    @Override
                    public void onCompleted() {
                        //dismissProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("loginFailed", e.toString()

                        );
                    }

                    @Override
                    public void onNext(RetMap user) {
                            ToastUtil.showToast(RegistActivityStep2.this,user.message);
                            if(user.status.equals("1")){
                                finish();
                            }
                    }
                });

    }




    @Override
    public int getLayoutId() {
        return R.layout.activity_next_regist;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }

    @OnClick({R.id.bt_get_code,R.id.btn_finish})
    public void onClicked(View view) {
        switch (view.getId()) {

            case R.id.bt_get_code:
                getcode();
                break;

            case R.id.btn_finish:
                checkcode();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(Tag);
    }
}
